<?php
/**
 * Model Ruleset
 * This is a ruleset just like Q_Rule_Set except that it is specifically
 * for setting up rules for a model. This allows us to set the model and
 * the record we are validating directly in the ruleset.
 *
 * @package     Visi
 * @subpackage  Model Validation
 * @author      Luke Visinoni <luke.visinoni@gmail.com>
 * @copyright   (c) 2010 Luke Visinoni <luke.visinoni@gmail.com>
 * @version     $Id$
 * @todo        When a model is saved and it has a ModelSet rule attached to it,
 *              it should require that it validates before allowing save.
 */
class Visi_Rule_ModelSet extends Q_Rule_Set {

    protected $_row;
    
    protected $_model;
    
    public function __construct(Visi_Db_Table $model, $row = null) {
    
        $this->setModel($model);
        if (!is_null($row)) {
            $this->setRow($row);
        }
    
    }
    
    public function setModel(Visi_Db_Table $model) {
    
        $this->_model = $model;
        return $this;
    
    }
    
    public function setRow(Visi_Db_Table_Row $row) {
    
        $this->_row = $row;
        return $this;
    
    }
    
    public function getModel() {
    
        return $this->_model;
    
    }
    
    public function getRow() {
    
        return $this->_row;
    
    }
    
    public function validate($data, $fieldsToValidate = null, $setErrors = true) {
    
        if (!parent::validate($data, $fieldsToValidate)) {
            if ($setErrors) {
                Visi_View_Helper_FormError::setErrors($this->getErrors());
            }
            return false;
        } else {
            return true;
        }
    
    }

}
